package agent;

import java.awt.geom.Rectangle2D;
import java.util.LinkedList;
import java.util.Random;

import behaviour.building.ReceiveConflictBehaviour;
import behaviour.building.ReceiveGoBackBehaviour;
import behaviour.building.ReceiveMoveRequestBehaviour;
import behaviour.building.SendInformationBehaviour;
import jade.core.AID;
import jade.core.Agent;
import jade.lang.acl.ACLMessage;

/**
 * 
 *
 */
public class BuildingAgent extends Agent {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 662330987251447505L;
	
	private LinkedList<Rectangle2D> oldPos;
	private Rectangle2D building;
		
	protected void setup() {
		Object[] args = getArguments();
		if (args != null && args.length > 0) {
			building = new Rectangle2D.Double((Double) args[0],(Double) args[1],(Double) args[2],(Double) args[3]);
			addBehaviour(new SendInformationBehaviour(new AID("GUI", AID.ISLOCALNAME), building, "create"));
			addBehaviour(new ReceiveConflictBehaviour());
			addBehaviour(new ReceiveMoveRequestBehaviour());
			addBehaviour(new ReceiveGoBackBehaviour());
		}
		System.out.println("Building: " + getAID());
		oldPos = new LinkedList<Rectangle2D>();
	}
	
	public void move(double x, double y, String action) {
		
		building.setRect(x, y, building.getWidth(), building.getHeight());
		addBehaviour(new SendInformationBehaviour(new AID("GUI", AID.ISLOCALNAME), building, action));
	}
	
	public void move2() {
		
		Random r = new Random();
		double alpha = (r.nextFloat() - 0.5)/4;
		building.setRect(building.getX() + alpha, building.getY() + alpha, building.getWidth(), building.getHeight());
		addBehaviour(new SendInformationBehaviour(new AID("GUI", AID.ISLOCALNAME), building, "modify"));
	}
	
	public void wasAskedToMove() {
		
		oldPos.addLast((Rectangle2D) building.clone());
		Random r = new Random();
		double alpha = (r.nextFloat() - 0.5)/4;
		building.setRect(building.getX() + alpha, building.getY() + alpha, building.getWidth(), building.getHeight());
		addBehaviour(new SendInformationBehaviour(new AID("GUI", AID.ISLOCALNAME), building, "forced-move"));
	}
	
	public void goBack() {
		
		if( ! oldPos.isEmpty()) {
			building = oldPos.removeLast();
			building.setRect(building.getX(), building.getY(), building.getWidth(), building.getHeight());
			addBehaviour(new SendInformationBehaviour(new AID("GUI", AID.ISLOCALNAME), building, "going-back"));
		}
		else {
			System.out.println(" - - - oldPos is empty : " + this.getName());
		}
	}
	
	public void clearPos() {
		oldPos = new LinkedList<Rectangle2D>();
	}

	protected void takeDown() {
		System.out.println(" - - - I'm going down... : " + this.getName());
		String content;

		double x = building.getX();
		double y = building.getY();
		double w = building.getWidth();
		double h = building.getHeight();

		content = x + "#" + y + "#" + w + "#" + h + "#" + "remove"; 
		ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
		msg.addReceiver(new AID("GUI", AID.ISLOCALNAME));
		msg.setConversationId("position-update");
		msg.setContent(content);
		send(msg);
		
		// Pour l'affichage
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ACLMessage msgRestart = new ACLMessage(ACLMessage.REQUEST);
		msgRestart.addReceiver(new AID("creator", AID.ISLOCALNAME));
		msgRestart.setConversationId("start-stop");
		msgRestart.setContent("start");
		send(msgRestart);
	}
	
	public Rectangle2D getBuildingRectangle(){
		return this.building;
	}
	
}
